package com.hcss.common.util;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

public class DateUtil {
    public static final String FORMAT_yMdHmsS = "yyyy-MM-dd HH:mm:ss.S";
    public static final String FORMAT_yMdHmsSSS = "yyyyMMddHHmmssSSS";
    // 格式：年－月－日 小时：分钟：秒
    public static final String FORMAT_yMdHms = "yyyy-MM-dd HH:mm:ss";
    // 格式：年－月－日 小时：分钟：秒
    public static final String FORMATyMdHms = "yyyy-MM-dd HH-mm-ss";
    public static final String FORMAT_yMdHmss = "yyyyMMddHHmmss";
    // 格式：年－月－日 小时：分钟
    public static final String FORMAT_yMdHm = "yyyy-MM-dd HH:mm";
    // 格式：年－月－日
    public static final String FORMAT_yMd = "yyyy-MM-dd";
    //格式：小时分钟秒
    public static final String FORMAT_Hmmss = "HHmmss";
    
    public static final String FORMAT_yMdHmm = "yyyyMMddHHmm";
    
    public static final String FORMAT_txt = "yyyyMMdd-HHmmss";
    
    public static final String FORMAT_yMd_txt = "yyyyMMdd";
    
    public static final String FORMAT_dMy_txt = "ddMMMyy";
    
    public static final String FORMAT_dM_txt = "ddMMM";
    
    public static final Long A_DAY = (long) (24 * 60 * 60 * 1000);
    
    public static final Map <Integer, String> DAY_OF_WEEK = new HashMap <Integer, String>();
    public static final Map <Integer, String> MON_ENG_MAP = new HashMap <Integer, String>();
    
    static {
        DAY_OF_WEEK.put(1, "星期日");
        DAY_OF_WEEK.put(2, "星期一");
        DAY_OF_WEEK.put(3, "星期二");
        DAY_OF_WEEK.put(4, "星期三");
        DAY_OF_WEEK.put(5, "星期四");
        DAY_OF_WEEK.put(6, "星期五");
        DAY_OF_WEEK.put(7, "星期六");
    }
    
    static {
        MON_ENG_MAP.put(1, "JAN");
        MON_ENG_MAP.put(2, "FEB");
        MON_ENG_MAP.put(3, "MAR");
        MON_ENG_MAP.put(4, "APR");
        MON_ENG_MAP.put(5, "MAY");
        MON_ENG_MAP.put(6, "JUN");
        MON_ENG_MAP.put(7, "JUL");
        MON_ENG_MAP.put(8, "AUG");
        MON_ENG_MAP.put(9, "SEP");
        MON_ENG_MAP.put(10, "OCT");
        MON_ENG_MAP.put(11, "NOV");
        MON_ENG_MAP.put(12, "DEC");
    }
    
    /**
     * 分钟加减
     *
     * @param date
     * @param num
     * @return
     */
    public static Date addMinute(Date date, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, num);
        return cal.getTime();
    }
    
    /**
     * 把日期转换为字符串
     *
     * @param date 日期
     * @return
     */
	public static String dateToString(Date date) {
		String result = null;
		SimpleDateFormat formater = new SimpleDateFormat(DateUtil.FORMAT_yMd);
		try {
			result = formater.format(date);
		} catch (Exception e) {
			e.printStackTrace();
            result = "";
        }
        return result;
    }
    
    /**
     *  把日期转换为字符串
     * @param date 日期
	 * @param format 转换格式
	 * @return
	 */
	public static String dateToString(Date date, String format) {
        String result = null;
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            result = formater.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            result = "";
        }
        return result;
    }
    
    /**
     * dateStr:Tue Feb 26 09:58:17 CST 2013
     * format:
     */
    public static String formatDate(String dateStr, String format) {
        try {
            //String dateStr = "Tue Feb 26 09:58:17 CST 2013";
            SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            System.out.println(sdf.format(sdf1.parse(dateStr)));
            return sdf.format(sdf1.parse(dateStr));
        } catch (Exception e) {
            return "";
        }
    }
    
    /**
     * 获取相对于date的日期  格式根据参数pattern转换
     *
     * @param dateStr
     * @param i       i>0往后i小时,i<0为往前i小时
     * @param pattern
     * @return
     */
    public static String getDateByCalendarFormat(String dateStr, int i, String pattern, int calendarFormat) {
        Date date = stringToDate(dateStr, FORMAT_yMdHms);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendarFormat, i);
        return new SimpleDateFormat(pattern).format(calendar.getTime());
    }
    
    /**
     * 获得当前日期字符串，默认格式"yyyy-MM-dd HH:mm:ss"
     *
     * @return
     */
    public static String getNow() {
        return dateToString(new Date(), FORMAT_yMdHms);
	}
	
	/**
	 * 获取指定格式的当前时间
	 * @param format 格式
	 * @return
     */
    public static String getNow(String format) {
        return dateToString(new Date(), format);
    }
    
    /**
     * 两个日期相减
     *
     * @return 相减得到的秒数
     */
    public static long getTimeDiff(String firstTime, String secTime) {
        long first = stringToDate(firstTime, FORMAT_yMdHms).getTime();
        long second = stringToDate(secTime, FORMAT_yMdHms).getTime();
        return (second - first) / 1000;
    }
    
    /**
     * 获取相对于date的日期  格式根据参数pattern转换
     *
     * @param date
     * @param i              i>0往后i小时,i<0为往前i小时
     * @param calendarFormat
     * @return
     */
    public static Date getDateByCalendarFormat(Date date, int i, int calendarFormat) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendarFormat, i);
        return calendar.getTime();
    }
    
    /**
     * 转换20190101为01Jan19
     *
     * @param dateStr
     * @return
     */
    public static String getDateByString(String dateStr) {
        Date date = stringToDate(dateStr, FORMAT_yMd_txt);
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FORMAT_dMy_txt, java.util.Locale.US);
        return sdf.format(date);
    }
    
    /**
     * 获取某年某月的天数
     */
    public static int getDaysOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * 获得当前日期
	 */
	public static int getToday() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DATE);
	}
	
	/**
	 * 获取今天的零点时间：2017-02-28 00:00:00.0
	 * @return
	 */
	public static Date getTodayStart(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		return calendar.getTime();
	}
	
	/**
	 * 获取某日的零点时间：2017-02-28 23:59:59.999
	 * @return
	 */
	public static Date getTodayStart(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		return calendar.getTime();
	}
	
	/**
	 * 获取今天的24点时间：yyyy-MM-dd 24:00:00.0
	 * @return
	 */
	public static Date getTodayEnd(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		
		return calendar.getTime();
	}
	
	/**
	 * 获取某日的24点时间：yyyy-MM-dd 24:00:00.0
	 * @return
	 */
	public static Date getTodayEnd(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		
		return calendar.getTime();
	}
	
	/**
	 * 获取相对于当天的日期  格式为"yyyy-MM-dd"
	 * @param i i>0往后i天,i<0为往前i天
	 * @return
	 */
	public static String getDateStr(int i) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, i);
		return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }
    
    public static Date getDate(int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, i);
        return calendar.getTime();
    }
    
    /**
     * 获取相对于date的日期  格式为"yyyy-MM-dd"
     *
     * @param i i>0往后i天,i<0为往前i天
     * @return
     */
    public static String getDate(Date date, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, i);
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }
    
    /**
     * 转换20190101为01Jan
     *
     * @param dateStr
     * @return
     */
    public static String getDateByString2(String dateStr) {
        Date date = stringToDate(dateStr, FORMAT_yMd_txt);
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FORMAT_dM_txt, java.util.Locale.US);
        return sdf.format(date);
    }
    
    /**
     * 根据字符串类型，获取字符串的格式
     *
     * @param date
     * @throws Exception
     */
    public static String getDateFormat(String date) throws Exception {
        Field[] fields = DateUtil.class.getDeclaredFields();
        if (EmptyUtil.isEmpty(fields) || EmptyUtil.isHaveEmpty(date)) {
            return null;
        }
        for (Field field : fields) {
            String format = String.valueOf(field.get(DateUtil.class));
            Date result = stringToDate(date, format);
            if (result != null) {
                return format;
            }
        }
        return null;
    }
    
    public static String getDateByCalendarFormat(int i, String pattern, int calendarFormat) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(calendarFormat, i);
		return new SimpleDateFormat(pattern).format(calendar.getTime());
	}
	
	/**
	 * 获得当前年份
	 */
	public static int getCurrYear() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.YEAR);
	}
	
	/**
	 * 返回日期的天
	 */
	public static int getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DATE);
	}
	
	/**
	 * 返回日期的年
	 */
	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
    
    /**
     * 返回日期的月份，1-12
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }
    
    /**
     * 把符合日期格式的字符串转换为日期类型
     *
     * @param dateStr 日期字符串
     * @param format  转换格式
     * @return
     */
    public static Date stringToDate(String dateStr, String format) {
        Date d = null;
        SimpleDateFormat formater = new SimpleDateFormat(format);
        
        try {
            formater.setLenient(false);
            d = formater.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
            d = null;
        }
        return d;
    }
    
    /**
     * 获取指定月份第一天
     *
     * @param
     * @return Date类型时间
     */
    public static String getFirstDayByMonth(int year,int month) {
		Calendar c = Calendar.getInstance();
		c.set(year, month - 1, 1);
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_yMd_txt);
		return sdf.format(c.getTime());
	}
	
	/**
	 * 计算两个日期相差的天数，如果date2 > date1 返回正数，否则返回负数
	 */
	public static long getDayDiff(Date date1, Date date2) {
		return (date2.getTime() - date1.getTime()) / 86400000;
	}
	
	/**
	 * 比较指定日期与当前日期的天差
	 */
	public static long getDayDiffCurr(String before) {
		return (System.currentTimeMillis() - stringToDate(before, FORMAT_yMd).getTime()) / 86400000;
	}
	
	/**
	 * 比较两个日期的年差
	 */
	public static int getYearDiff(String before, String after) {
		Date beforeDay = stringToDate(before, FORMAT_yMd);
		Date afterDay = stringToDate(after, FORMAT_yMd);
		return getYear(afterDay) - getYear(beforeDay);
	}
	
	/**
	 * 比较指定日期与当前日期的年差
	 */
	public static int getYearDiffCurr(String after) {
		return getYear(new Date()) - getYear(stringToDate(after, FORMAT_yMd));
	}
	
	/**
	 * 获取每月的第一周
	 */
	public static int getFirstWeekdayOfMonth(int year, int month) {
		Calendar c = Calendar.getInstance();
		c.setFirstDayOfWeek(Calendar.SATURDAY); // 星期天为第一天
		c.set(year, month - 1, 1);
		return c.get(Calendar.DAY_OF_WEEK);
	}
    
    /**
     * 获取每月的最后一周
     */
    public static int getLastWeekdayOfMonth(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.SATURDAY); // 星期天为第一天
        c.set(year, month - 1, getDaysOfMonth(year, month));
        return c.get(Calendar.DAY_OF_WEEK);
    }
    
    /**
     * 获取指定年的第一天
	 * @param
	 * @return
	 */
	public static String getFirstDayByYear(int year) {
		Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.YEAR, year);
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_yMd_txt);
        return sdf.format(cal.getTime());
    }
    
    /**
     * 导出txt要的时间格式
     *
     * @return
     */
    public static String getTxtDate() {
        return dateToString(new Date(), FORMAT_txt);
    }
    
    /**
     * 获取时间  半年  上半年返回1   下半年返回2
     *
     * @param date
     * @return
     */
    public static int getHY(Date date){
		int hy=0;
		int month=getMonth(date);
		switch(month){
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
				hy=1;
				break;
			case 7:
			case 8:
			case 9:
			case 10:
			case 11:
			case 12:
				hy=2;
				break;
			default:break;
		}
		return hy;
	}
	
	/**
     * 获取当前日期过去一个月的开始日期
     * */
    public static String getMonthStartDay(String format) {
        format = StringUtils.isEmpty(format) ? FORMAT_yMd : format;
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.add(Calendar.MONTH, -1);
        String start = new SimpleDateFormat(format).format(cal.getTime());
        return start;
    }
    
    public static String getMonthLastDay(String format) {
        format = StringUtils.isEmpty(format) ? FORMAT_yMd : format;
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String end = new SimpleDateFormat(format).format(cal.getTime());
        return end;
    }
    
    /**
     * 获取指定月份最后一天
     * @param
	 * @return
	 */
	public static String getLastDayByMonth(int year,int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, 1);
		cal.add(Calendar.DATE, -1);
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_yMd_txt);
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 月份加减
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date addMonth(Date date, int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, num);
		return cal.getTime();
	}
	
	/**
	 * 天加减
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date addDay(Date date, int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
        cal.add(Calendar.DATE, num);
        return cal.getTime();
    }
    
    /**
     * 24小时制-小时加减
     *
     * @param date
     * @param num
     * @return
     */
    public static Date addHour(Date date, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, num);
        return cal.getTime();
    }
    
    /**
     * 获取指定年的最后一天
     *
     * @param
     * @return
     */
    public static String getLastDayByYear(int year) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.YEAR, year);
        cal.roll(Calendar.DAY_OF_YEAR, -1);
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_yMd_txt);
        return sdf.format(cal.getTime());
    }
    
    /**
     * 获取指定时间的上一个月时间
     *
     * @param
     * @return
     */
    public static String getLastMonth(String dateStr) {
        Date date = stringToDate(dateStr, FORMAT_yMd_txt);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_yMd_txt);
        return sdf.format(cal.getTime());
    }
    
    /**
     * 获取指定时间的上一个月时间
     *
     * @param
     * @return
     */
    public static String getLastYear(String dateStr) {
        Date date = stringToDate(dateStr, FORMAT_yMd_txt);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR, -1);
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_yMd_txt);
        return sdf.format(cal.getTime());
    }
    
    public static String getLastYearPeriod(String dateStr) {
        String rst = "";
        //2014-02-02 ~ 2014-02-02
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(dateStr.substring(0, 4)), Integer.parseInt(dateStr.substring(5, 7)) - 1,
                Integer.parseInt(dateStr.substring(8, 10)));
        cal.add(Calendar.YEAR, -1);
        rst = new SimpleDateFormat(FORMAT_yMd).format(cal.getTime());
        
        cal.set(Integer.parseInt(dateStr.substring(13, 17)), Integer.parseInt(dateStr.substring(18, 20)) - 1,
                Integer.parseInt(dateStr.substring(21, 23)));
        cal.add(Calendar.YEAR, -1);
        rst = rst + " ~ " + new SimpleDateFormat(FORMAT_yMd).format(cal.getTime());
        return rst;
    }
    
    /**
     * 获取最大时间
     *
     * @param dates
     * @return
     */
    public static Date getMaxDate(Date...dates){
		if (dates == null || dates.length == 0){
			return null;
		}
		Date minDate = dates[0];
		for (int i = 1, length = dates.length; i < length; i++){
			Long minL = minDate.getTime();
			Long l = dates[i].getTime();
			if (l > minL){
				minDate = dates[i];
			}
		}
		return minDate;
	}
	
	/**
	 * 获取中国式星期 1-7：星期一-日
	 * @param date
	 * @return
	 */
	public static Integer getChineseDayOfWeek(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Integer dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        return ((dayOfWeek - 1 > 0) ? dayOfWeek - 1 : 7);
    }
    
    /**
     * 获取星期
     *
     * @param date
     * @return
     */
    public static String getDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Integer dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        return DAY_OF_WEEK.get(dayOfWeek);
    }
    
    /**
     * 判断是不是周末
     *
     * @param date
     * @return
     */
    public static boolean isWeekend(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Integer dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        return dayOfWeek == 7 || dayOfWeek == 1;
    }
    
    /**
     * 获取最小时间
     *
     * @param dates
     * @return
     */
    public static Date getMinDate(Date... dates) {
        if (dates == null || dates.length == 0) {
            return null;
        }
        Date minDate = dates[0];
        for (int i = 1, length = dates.length; i < length; i++) {
            Long minL = minDate.getTime();
            Long l = dates[i].getTime();
            if (l < minL) {
                minDate = dates[i];
            }
        }
        return minDate;
    }
    
    /**
     * 获取相对于date的月份
     *
     * @param i i>0往后i月,i<0为往前i月
     * @return
     */
    public static String getMonth(Date date, int i, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, i);
        return new SimpleDateFormat(format).format(calendar.getTime());
    }
    
    /**
     * 获取相对于当前月份的月份日期
     *
     * @param i i>0往后i月,i<0为往前i月
     * @return
     */
    public static String getMonth(int i, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, i);
        return new SimpleDateFormat(format).format(calendar.getTime());
    }
    
    /**
     * 获取月份英文简写
     *
     * @param dateStr
     * @return
     */
    public static String getMonthEnglish(String dateStr) {
        Date date = stringToDate(dateStr, FORMAT_yMd_txt);
        int month = getMonth(date);
        return MON_ENG_MAP.get(month);
    }
    
    public static int getSeason(Date date) {
        int season = 0;
        int month = getMonth(date);
        switch (month) {
            case 1:
            case 2:
            case 3:
                season = 1;
                break;
            case 4:
            case 5:
            case 6:
                season = 2;
                break;
            case 7:
            case 8:
            case 9:
                season = 3;
                break;
            case 10:
            case 11:
            case 12:
                season = 4;
                break;
            default:
                break;
        }
        return season;
    }
    
    /**
     * 两个日期相减
     *
     * @return 相减得到毫秒
     */
    public static long getTimeDiff_S(String firstTime, String secTime) {
        long first = stringToDate(firstTime, FORMAT_yMdHmsSSS).getTime();
        long second = stringToDate(secTime, FORMAT_yMdHmsSSS).getTime();
        return second - first;
    }
    
    /**
     * 判断日期是否有效,包括闰年的情况
     *
     * @param date YYYY-mm-dd
     * @return
     */
    public static boolean isDate(String date) {
        StringBuffer reg = new StringBuffer("^((\\d{2}(([02468][048])|([13579][26]))-?((((0?");
        reg.append("[13578])|(1[02]))-?((0?[1-9])|([1-2][0-9])|(3[01])))");
        reg.append("|(((0?[469])|(11))-?((0?[1-9])|([1-2][0-9])|(30)))|");
        reg.append("(0?2-?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12");
        reg.append("35679])|([13579][01345789]))-?((((0?[13578])|(1[02]))");
        reg.append("-?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");
        reg.append("-?((0?[1-9])|([1-2][0-9])|(30)))|(0?2-?((0?[");
        reg.append("1-9])|(1[0-9])|(2[0-8]))))))");
        Pattern p = Pattern.compile(reg.toString());
        return p.matcher(date).matches();
    }
    
    /**
     * 时间戳转行成时间字符串
     *
     * @param timeStamp
     * @return
     */
    public static String longToDateStr(Long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_yMdHms);
        String sd = sdf.format(new Date(timeStamp));   // 时间戳转换成时间
        return sd;
    }
    
    public static void main(String[] args) {
        long time = getTimeDiff_SSS("20190708142130333", "20190708142330610");
        System.out.println(time);
        long days = DateUtil.getDayDiff(DateUtil.stringToDate("2017-06-12", DateUtil.FORMAT_yMd), DateUtil.stringToDate("2017-06-19", DateUtil.FORMAT_yMd));
		System.out.println(days);
	}
	
	/**
	 * 两个日期相减
	 * @return 相减得到的秒数
	 */
	public static long getTimeDiff_SSS(String firstTime, String secTime) {
		long first = stringToDate(firstTime, FORMAT_yMdHmsSSS).getTime();
		long second = stringToDate(secTime, FORMAT_yMdHmsSSS).getTime();
		return (second - first) / 1000;
	}
}
